﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Xml.Serialization;

namespace Isaac.Joy.Menu.Module
{
    public class Menu : IMenu
    {
        public string Name { get; set; }

        public string Icon { get; set; }

        public MenuAction Action { get; set; }

        public bool IsLeaf
        {
            get { return !Childrens.Any(); }
        }

        public int Order { get; set; }

        public string Permission { get; set; }

        [XmlIgnore]
        public IEnumerable<IMenu> Childrens
        {
            get { return Menus; }
        }

        public List<Menu> Menus { get; set; }

        public void Add(IMenu menu)
        {
            if (Menus == null) return;

            var menuobj = menu as Menu;
            if(menuobj != null) Menus.Add(menuobj);
        }

        public int CompareTo(object obj)
        {
            try
            {
                var menu = obj as IMenu;
                var result = menu != null && Order > menu.Order ? 0 : 1;
                return result;
            }
            catch (Exception) { return 0; }
        }
    }
}
